home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / EXPATH.C < prev    next >
C/C++ Source or Header  |  1991-11-16  |  7KB  |  160 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    e x p a t h . c                                                 */
  3. /*                                                                    */
  4. /*    Path expansion functions for UUPC/extended                      */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*                    MS-DOS and OS/2 header files                    */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <direct.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17.  
  18. /*--------------------------------------------------------------------*/
  19. /*                     UUPC/extended header files                     */
  20. /*--------------------------------------------------------------------*/
  21.  
  22. #include "lib.h"
  23. #include "expath.h"
  24. #include "hlib.h"
  25. #include "hostable.h"
  26. #include "security.h"
  27. #include "usertabl.h"
  28.  
  29. /*--------------------------------------------------------------------*/
  30. /*   e x p a n d _ p a t  h                                           */
  31. /*                                                                    */
  32. /*   Expands ~, ~/ and relative paths                                 */
  33. /*--------------------------------------------------------------------*/
  34.  
  35. char *expand_path(char *path,          /* Input/output path name     */
  36.                   const char *cur_dir, /* Default directory path     */
  37.                   const char *home,    /* Default home directory     */
  38.                   const char *ftype )  /* Default extension          */
  39. {
  40.    char        *p, *fname;
  41.    char        save[FILENAME_MAX];
  42.    struct UserTable *userp;
  43.  
  44. /*--------------------------------------------------------------------*/
  45. /*                   Convert backslashes to slashes                   */
  46. /*--------------------------------------------------------------------*/
  47.  
  48.    p  = path;
  49.    while ((p = strchr(p,'\\')) != NULL)
  50.       *p++ = '/';
  51.  
  52. /*--------------------------------------------------------------------*/
  53. /*                 Add optional extension, if needed                  */
  54. /*--------------------------------------------------------------------*/
  55.  
  56.    if ( ftype != NULL )
  57.    {
  58.       p = strrchr(path,'/');  /* Get the last slash in name          */
  59.  
  60.       if ( p == NULL )        /* No slash?                           */
  61.          p = path;            /* Okay, look at entire name           */
  62.  
  63.       if ( strchr( p , '.') == NULL )  /* Does name have a period?   */
  64.          strcat( strcat(p, ".") ,ftype );
  65.                               /* No --> Add extension                */
  66.    } /* if ( ftype != NULL ) */
  67.  
  68. /*--------------------------------------------------------------------*/
  69. /*               If a fully qualified path name, return               */
  70. /*--------------------------------------------------------------------*/
  71.  
  72.    if (*path == '/')
  73.       return path;            /* nothing to do */
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*      If non-default drive and not full path, reject the path       */
  77. /*--------------------------------------------------------------------*/
  78.  
  79.    if (isalpha( *path ) && (path[1] == ':'))
  80.    {
  81.       if (path[2] == '/')     /* Absolute path on drive?             */
  82.          return path;         /* Yes --> Leave it alone              */
  83.  
  84.       printmsg(0, "expand_path: Invalid path \"%s\"; \
  85. relative path on non-default drive.  (Use full path for file.)",path);
  86.       return NULL;          /* nothing to do  */
  87.    } /* if */
  88.    else
  89.       p = path;               /* Copy entire path                    */
  90.  
  91. /*--------------------------------------------------------------------*/
  92. /*            Try to translate the file as a home directory path      */
  93. /*--------------------------------------------------------------------*/
  94.  
  95.    strcpy(save, p);
  96.    if (save[0] == '~')  {
  97.       if (save[1] == '/')  {
  98.          strcpy(path, home);  /* Use home dir for this user          */
  99.          fname = save + 2;    /* Step past directory for simple name */
  100.       }
  101.       else  {
  102.          if ((fname = strchr(save + 1, '/')) == NULL)
  103.          {
  104.             printmsg(0,"expand_path: path \"%s\" illegal",p);
  105.             return NULL;
  106.          }
  107.  
  108. /*--------------------------------------------------------------------*/
  109. /*                Look in /etc/passwd for the user id                 */
  110. /*--------------------------------------------------------------------*/
  111.  
  112.          *fname++ = '\0';           /* End string, step past it */
  113.          userp = checkuser(save + 1);  /* Locate user id in table  */
  114.          if ( userp == BADUSER )    /* Invalid user id?         */
  115.          {                          /* Yes --> Dump in trash    */
  116.             printmsg(0,"expand_path: User \"%s\" is invalid", save + 1);
  117.             return NULL;
  118.          } /* if */
  119.          strcpy(path, userp->homedir);
  120.       } /* else */
  121.    } /* if (save[0] == '~')  */
  122.  
  123. /*--------------------------------------------------------------------*/
  124. /*    No user id appears in the path; just append the input data      */
  125. /*    to the current directory to convert the relative path to an     */
  126. /*    absolute path                                                   */
  127. /*--------------------------------------------------------------------*/
  128.  
  129.    else {
  130.          fname = save;              /* Give it the file name - 6/23/91  */
  131.          if ( cur_dir == NULL )
  132.             getcwd( path, FILENAME_MAX);
  133.          else if ( equal(cur_dir,"."))
  134.          {
  135.             strcpy( path, save );
  136.             return path;
  137.          }
  138.          else
  139.             strcpy( path, cur_dir );
  140.    } /* else */
  141.  
  142. /*--------------------------------------------------------------------*/
  143. /*             Normalize the path, and then add the name              */
  144. /*--------------------------------------------------------------------*/
  145.  
  146.    while ((p = strchr(p,'\\')) != NULL)
  147.       *p++ = '/';
  148.    if ( path[ strlen( path ) - 1 ] != '/' )
  149.       strcat( path, "/");
  150.    strlwr( path );            /* Can lower case path, but not the
  151.                                  name because name may be UNIX!      */
  152.    strcat( path, fname );
  153.  
  154. /*--------------------------------------------------------------------*/
  155. /*                       Return data to caller                        */
  156. /*--------------------------------------------------------------------*/
  157.  
  158.    return path;
  159. } /* expand_path */
  160.